package com.atjg.autocat.facelib.manger

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.hardware.usb.UsbDevice
import com.ai.face.base.utils.DataConvertUtils
import com.atjg.autocat.utils.CameraUtils
import com.atjg.autocat.utils.ConfigUtils
import com.atjg.autocat.uvccamera.UVCCameraProxy
import com.atjg.autocat.uvccamera.bean.PicturePath
import com.atjg.autocat.uvccamera.callback.ConnectCallback
import java.nio.ByteBuffer


class UVCCameraManager(var cameraBuilder: CameraBuilder) {
    // 配置UVC 协议摄像头默认的分辨率，请参考你的摄像头能支持的分辨率，分辨率不用那么高关键在成像能力
    // 分辨率太高需要高性能的硬件配置。强烈建议摄像头的宽动态值 > 105DB

    private val UVC_CAMERA_WIDTH: Int = 640

    private val UVC_CAMERA_HEIGHT: Int = 480

    //默认匹配的摄像头关键字，但并不是所有的摄像头命名都规范会带有这种关键字样

    private var faceAIAnalysisCallBack: OnFaceAIAnalysisCallBack? = null
    private var onCameraStatuesCallBack: OnCameraStatusCallBack? = null
    private val context: Context = cameraBuilder.context.applicationContext;
    private var width = UVC_CAMERA_WIDTH
    private var height = UVC_CAMERA_HEIGHT
    private var mUVCCamera0: UVCCameraProxy? = null
    private var reuseBitmap: Bitmap? = null
    private var cameraUtils0: CameraUtils? = null
    init {
        initUVCCamera()
    }

    private fun initUVCCamera() {

        val box1Usb = ConfigUtils.query<String>("faceUsb") ?: "-1"
        if (box1Usb == "-1") {
            return
        }
        cameraUtils0 = CameraUtils(
            context, "0", cameraBuilder.cameraView, "${context.externalCacheDir}"
        ) { path ->

        }
        cameraUtils0?.reopenCamera()
        return
//        mUVCCamera0 = UVCCameraProxy(context)
//        // 已有默认配置，不需要可以不设置
//        mUVCCamera0?.config?.isDebug(true)
//            ?.setPicturePath(PicturePath.APPCACHE)
//            ?.setDirName("uvccamera")
//            //根据这里确定哪个摄像头
//            ?.setProductId(box1Usb.toInt())
//        if (mUVCCamera0?.isCameraOpen == true){
//            mUVCCamera0?.setPreviewSize(640, 480)
//            mUVCCamera0?.startPreview()
//            return
//        }
//        mUVCCamera0?.setPreviewTexture(cameraBuilder.cameraView)
//        mUVCCamera0?.setConnectCallback(object : ConnectCallback {
//            override fun onAttached(usbDevice: UsbDevice?) {
//                onCameraStatuesCallBack?.onAttach(usbDevice)
//                mUVCCamera0?.requestPermission(usbDevice)
//            }
//
//            override fun onGranted(usbDevice: UsbDevice?, granted: Boolean) {
//                if (granted) {
//                    mUVCCamera0?.connectDevice(usbDevice)
//                }
//            }
//
//            @SuppressLint("SetTextI18n")
//            override fun onConnected(usbDevice: UsbDevice?) {
//              mUVCCamera0?.openCamera()
//                onCameraStatuesCallBack?.onDeviceOpen(usbDevice, true)
//
//            }
//
//            override fun onCameraOpened() {
//                mUVCCamera0?.setPreviewSize(640, 480)
//                mUVCCamera0?.startPreview()
//
//            }
//
//            override fun onDetached(usbDevice: UsbDevice?) {
//                mUVCCamera0?.closeCamera()
//            }
//        })
        mUVCCamera0?.setPreviewCallback {
            reuseBitmap = DataConvertUtils.NV21Data2Bitmap(
                ByteBuffer.wrap(it), width, height,
                cameraBuilder.degree, 3, cameraBuilder.isHorizontalMirror
            )
            faceAIAnalysisCallBack?.onBitmapFrame(reuseBitmap)
        }

    }


    /**
     * 设置回调,给人脸识别SDK分析每帧数据，帧率15～30
     */
    fun setFaceAIAnalysis(callBack: OnFaceAIAnalysisCallBack) {
        faceAIAnalysisCallBack = callBack
    }

    /**
     * 使用结束后, 释放 camera
     *
     */
    fun releaseCameraHelper() {
        mUVCCamera0?.stopPreview()
//        mUVCCamera0?.closeCamera()
        faceAIAnalysisCallBack = null
        onCameraStatuesCallBack = null // 添加这行
    }

    interface OnFaceAIAnalysisCallBack {
        fun onBitmapFrame(bitmap: Bitmap?)
    }

    interface OnCameraStatusCallBack {
        fun onAttach(device: UsbDevice?)
        fun onDeviceOpen(device: UsbDevice?, isFirstOpen: Boolean)
    }

}